package com.highcom.controller;

import com.highcom.common.QiniuUtils;
import com.highcom.common.RedisConstant;
import com.highcom.config.RedisConfig;
import com.highcom.constant.MessageConstant;
import com.highcom.entity.PageResult;
import com.highcom.entity.QueryPageBean;
import com.highcom.entity.Result;
import com.highcom.mapper.TSetmealMapper;
import com.highcom.model.CheckGroup;
import com.highcom.model.Setmeal;
import com.highcom.model.TSetmeal;
import com.highcom.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * @version 1.0
 * @Author YangJiaQuan_hrbnu
 * @Date 2024/8/1 14:50
 */
@RestController
@RequestMapping("/setmeal")
public class SetMealController {
    private static JedisPool jedisPool;
    @Autowired
    private RedisConfig redisConfig;
    @PostConstruct
    public void init(){
        jedisPool = redisConfig.redisPoolFactory();
    }
    @Resource
    private SetMealService setMealService;

    @RequestMapping("/upload")
    public Result upload(MultipartFile imgFile){
        String originalFilename = imgFile.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        String ex = originalFilename.substring(index-1);

        String fileName = UUID.randomUUID().toString()+ex;

        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            //  将上传图片统一上传到七牛云服务器上，实现图片回显功能，并使用redis配合数据库识别垃圾图片进行删除
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody TSetmeal tSetmeal, Integer[] checkgroupIds){
        try{
            setMealService.add(tSetmeal,checkgroupIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = setMealService.pageQuery(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        Setmeal setmeal = setMealService.findById(id);
        if (setmeal != null){
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        }
        return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
    }

    @RequestMapping("/findCheckGroupBySetmealId")
    public Result findCheckGroupBySetmealId(Integer id){
        try {
            List<Integer> checkgroupIds =  setMealService.findCheckGroupBySetmealId(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkgroupIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal,Integer [] checkgroupIds){
        try {
            setMealService.edit(setmeal,checkgroupIds);
            return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            Setmeal setmeal = setMealService.findById(id);

            QiniuUtils.deleteFileFromQiniu(setmeal.getImg());
            System.out.println("已经删除七牛云中的图片"+setmeal.getImg());
            setMealService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<TSetmeal> setmeals = setMealService.findAll();
            return new Result(true,MessageConstant.QUERY_SETMEALLIST_SUCCESS,setmeals);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
    }

    @RequestMapping("/findByIdAndCheckgroupsAndCheckItems")
    public Result findByIdAndCheckgroupsAndCheckItems(Integer id){
        try{
            TSetmeal tSetmeal = setMealService.findByIdAndCheckgroupsAndCheckItems(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,tSetmeal);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

}
